package com.zlzxm.shoesmarket.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.net.repository.InvRepository;
import com.zlzxm.shoesmarket.net.request.RqInvAdd;
import com.zlzxm.shoesmarket.net.respone.BaseResponse;
import com.zlzxm.shoesmarket.util.RxAndroidHelp;
import com.zlzxm.shoesmarket.view.adapter.entity.EntityOperateSize;
import com.zlzxm.shoesmarket.viewmodel.entity.EntityTransfer;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;

/**
 * CREATE BY zlzxm ON 4/20/21
 */
public class VMBatchInOut extends BaseViewModel {

    InvRepository invRepository  = new InvRepository();

    public MutableLiveData<EntityTransfer> liveGoodsInfo = new MutableLiveData<>();

    public MutableLiveData<List<EntityOperateSize>> liveOperateSize = new MutableLiveData<>();



    public void editSize(String id,boolean show){

        List<EntityOperateSize> list =  liveOperateSize.getValue();

        if(list == null){

            list = new ArrayList<>();
        }

        if(show){

            EntityOperateSize entityOperateSize = new EntityOperateSize();
            entityOperateSize.setNote("备注");
            entityOperateSize.setNum(0);
            entityOperateSize.setPrice("0");
            entityOperateSize.setSize(id);

            list.add(entityOperateSize);

        }else {

            for (EntityOperateSize entityOperateSize : list) {

                if (entityOperateSize.getSize().equals(id)){

                    list.remove(entityOperateSize);
                }
            }

        }

        liveOperateSize.setValue(list);

    }


    public void requestAdd(){


        List<EntityOperateSize>  list  =  liveOperateSize.getValue();

        for (EntityOperateSize entityOperateSize : list) {

            EntityTransfer entityTransfer = liveGoodsInfo.getValue();

            RqInvAdd rqInvAdd = new RqInvAdd(entityTransfer.getCode(),"7" ,entityOperateSize.getNum()+"",
                    entityOperateSize.getPrice(),entityOperateSize.getNote(),entityOperateSize.getSize());

            invRepository.add(rqInvAdd )
                    .compose(RxAndroidHelp.turn())
                    .subscribe(new Consumer<BaseResponse<String>>() {
                        @Override
                        public void accept(BaseResponse<String> stringBaseResponse) throws Throwable {

                        }
                    },throwableCallback);


        }


    }


    //<editor-fold defaultState="collapsed" desc="测试">




    private List<EntityOperateSize> createTestInfoData(){

        List<EntityOperateSize> list = new ArrayList<>();

        for (int i = 0; i < 10; i++) {

            EntityOperateSize entityOperateSize = new EntityOperateSize();

            entityOperateSize.setNote("sss");
            entityOperateSize.setNum(i+1);
            entityOperateSize.setPrice(""+i);
            entityOperateSize.setSize(""+i);


            list.add(entityOperateSize);
        }




        return  list;

    }




    // </editor-fold>
}
